package com.archetypus.projectschedule.core.involvements.sf;

import javax.transaction.UserTransaction;

import com.archetypus.projectschedule.common.ServiceLocator;
import com.archetypus.projectschedule.common.ServiceLocatorException;
import com.archetypus.projectschedule.common.data.DAOFactory;
import com.archetypus.projectschedule.common.data.involvements.InvolvementDAO;
import com.archetypus.projectschedule.common.dto.activities.Activity;
import com.archetypus.projectschedule.common.dto.involvements.Involvement;
import com.archetypus.projectschedule.common.dto.involvements.InvolvementListHandler;
import com.archetypus.projectschedule.common.dto.people.Person;
import com.archetypus.projectschedule.common.exceptions.ProjectSchedulerException;

public class InvolvementSF implements IInvolvementSF {
	
	private static InvolvementSF instance;
	
	protected InvolvementSF(){}
	
	public static InvolvementSF getInstance(){
		if(instance == null)
			instance = new InvolvementSF();
		
		return instance;
	}
	
	@Override
	public void createInvolvement(Involvement involvement) throws ProjectSchedulerException {
		UserTransaction userTransaction = null;
		
		try {
			userTransaction = ServiceLocator.getInstance().lookupUserTransaction();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
			throw new ProjectSchedulerException("Could not create transaction object",e);
		}
		
		try {
			userTransaction.begin();
			DAOFactory daoFactory = DAOFactory.getDAOFactory();
			InvolvementDAO involvementDAO = daoFactory.getInvolvementDAO();
			involvementDAO.insert(involvement);
			userTransaction.commit();
		} catch (Exception e) {
			try{
				userTransaction.rollback();
			}catch(Exception ex){
				ex.printStackTrace();
				throw new ProjectSchedulerException("Could not rollback transaction",ex);
			}
			e.printStackTrace();
			throw new ProjectSchedulerException(e.getMessage(),e);
		}
	}

	@Override
	public void deleteInvolvement(Involvement involvement) throws ProjectSchedulerException {
		UserTransaction userTransaction = null;
		
		try {
			userTransaction = ServiceLocator.getInstance().lookupUserTransaction();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
			throw new ProjectSchedulerException("Could not create transaction object",e);
		}
		
		try {
			userTransaction.begin();
			DAOFactory daoFactory = DAOFactory.getDAOFactory();
			InvolvementDAO involvementDAO = daoFactory.getInvolvementDAO();
			involvementDAO.delete(involvement);
			userTransaction.commit();
		} catch (Exception e) {
			try{
				userTransaction.rollback();
			}catch(Exception ex){
				ex.printStackTrace();
				throw new ProjectSchedulerException("Could not rollback transaction",ex);
			}
			e.printStackTrace();
			throw new ProjectSchedulerException(e.getMessage(),e);
		}

	}

	@Override
	public Involvement findInvolvement(Involvement involvement) throws ProjectSchedulerException {
		UserTransaction userTransaction = null;
		Involvement retrievedInvolvement = null;
		
		try {
			userTransaction = ServiceLocator.getInstance().lookupUserTransaction();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
			throw new ProjectSchedulerException("Could not create transaction object",e);
		}
		
		try {
			userTransaction.begin();
			DAOFactory daoFactory = DAOFactory.getDAOFactory();
			InvolvementDAO involvementDAO = daoFactory.getInvolvementDAO();
			retrievedInvolvement = involvementDAO.find(involvement);
			userTransaction.commit();
		} catch (Exception e) {
			try{
				userTransaction.rollback();
			}catch(Exception ex){
				ex.printStackTrace();
				throw new ProjectSchedulerException("Could not rollback transaction",ex);
			}
			e.printStackTrace();
			throw new ProjectSchedulerException(e.getMessage(),e);
		}
		return retrievedInvolvement;
	}

	@Override
	public InvolvementListHandler listInvolvements() throws ProjectSchedulerException {
		UserTransaction userTransaction = null;
		InvolvementListHandler listHandler = new InvolvementListHandler();
		
		try {
			userTransaction = ServiceLocator.getInstance().lookupUserTransaction();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
			throw new ProjectSchedulerException("Could not create transaction object",e);
		}
		
		try {
			userTransaction.begin();
			DAOFactory daoFactory = DAOFactory.getDAOFactory();
			InvolvementDAO involvementDAO = daoFactory.getInvolvementDAO();
			listHandler.setList(involvementDAO.list());
			userTransaction.commit();
		} catch (Exception e) {
			try{
				userTransaction.rollback();
			}catch(Exception ex){
				ex.printStackTrace();
				throw new ProjectSchedulerException("Could not rollback transaction",ex);
			}
			e.printStackTrace();
			throw new ProjectSchedulerException(e.getMessage(),e);
		}
		return listHandler;
	}
	
	@Override
	public InvolvementListHandler listInvolvementsByActivity(Activity activity) throws ProjectSchedulerException {
		UserTransaction userTransaction = null;
		InvolvementListHandler listHandler = new InvolvementListHandler();
		
		try {
			userTransaction = ServiceLocator.getInstance().lookupUserTransaction();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
			throw new ProjectSchedulerException("Could not create transaction object",e);
		}
		
		try {
			userTransaction.begin();
			DAOFactory daoFactory = DAOFactory.getDAOFactory();
			InvolvementDAO involvementDAO = daoFactory.getInvolvementDAO();
			listHandler.setList(involvementDAO.listByActivity(activity));
			userTransaction.commit();
		} catch (Exception e) {
			try{
				userTransaction.rollback();
			}catch(Exception ex){
				ex.printStackTrace();
				throw new ProjectSchedulerException("Could not rollback transaction",ex);
			}
			e.printStackTrace();
			throw new ProjectSchedulerException(e.getMessage(),e);
		}
		return listHandler;
	}

	@Override
	public InvolvementListHandler listInvolvementsByPerson(Person person) throws ProjectSchedulerException {
		UserTransaction userTransaction = null;
		InvolvementListHandler listHandler = new InvolvementListHandler();
		
		try {
			userTransaction = ServiceLocator.getInstance().lookupUserTransaction();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
			throw new ProjectSchedulerException("Could not create transaction object",e);
		}
		
		try {
			userTransaction.begin();
			DAOFactory daoFactory = DAOFactory.getDAOFactory();
			InvolvementDAO involvementDAO = daoFactory.getInvolvementDAO();
			listHandler.setList(involvementDAO.listByPerson(person));
			userTransaction.commit();
		} catch (Exception e) {
			try{
				userTransaction.rollback();
			}catch(Exception ex){
				ex.printStackTrace();
				throw new ProjectSchedulerException("Could not rollback transaction",ex);
			}
			e.printStackTrace();
			throw new ProjectSchedulerException(e.getMessage(),e);
		}
		return listHandler;
	}
	
	@Override
	public void updateInvolvement(Involvement involvement) throws ProjectSchedulerException {
		UserTransaction userTransaction = null;
		
		try {
			userTransaction = ServiceLocator.getInstance().lookupUserTransaction();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
			throw new ProjectSchedulerException("Could not create transaction object",e);
		}
		
		try {
			userTransaction.begin();
			DAOFactory daoFactory = DAOFactory.getDAOFactory();
			InvolvementDAO involvementDAO = daoFactory.getInvolvementDAO();
			involvementDAO.update(involvement);
			userTransaction.commit();
		} catch (Exception e) {
			try{
				userTransaction.rollback();
			}catch(Exception ex){
				ex.printStackTrace();
				throw new ProjectSchedulerException("Could not rollback transaction",ex);
			}
			e.printStackTrace();
			throw new ProjectSchedulerException(e.getMessage(),e);
		}

	}
}
